package com.lcw.lostandfound.controller;

import com.lcw.lostandfound.pojo.Comment;
import com.lcw.lostandfound.service.CommentService;
import com.lcw.lostandfound.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class CommentController {
    @Autowired
    private CommentService commentService;

    @RequestMapping("/comments")
    public String comments(Model model, @RequestParam(name = "goodsId") Integer goodsId){
        List<Comment> comments = commentService.listCommentByGoodsId(goodsId);
        System.out.println(comments);
        model.addAttribute("comments",comments);
        System.out.println(123);
        return "comment";
    }
    @RequestMapping("/commentsToSave")
    public String post(Comment comment,Model model){
        System.out.println(comment.toString());
        commentService.saveComment(comment);
        model.addAttribute("comments",commentService.listCommentByGoodsId(comment.getGoodsId()));
        return "comment";
    }
    @ResponseBody
    @RequestMapping("/removeComment")
    public String removeComment(Integer id){
        System.out.println(id);
        if (commentService.deleteComment(id)>0){
            return "true";
        }
        return "false";
    }
}
